package com.gph.saviorframework.wf.utils;

import java.util.ArrayList;
import java.util.List;

public class ListUtils {

    public static <T> List<List<T>> splitList(List<T> list, int pageSize) {
        int listSize = list.size();
        int page = (listSize + (pageSize - 1)) / pageSize;

        List<List<T>> listArray = new ArrayList<List<T>>();
        for (int i = 0; i < page; i++) {
            List<T> subList = new ArrayList<T>();
            for (int j = 0; j < listSize; j++) {
                int pageIndex = ((j + 1) + (pageSize - 1)) / pageSize;
                if (pageIndex == (i + 1)) {
                    subList.add(list.get(j));
                }
                if ((j + 1) == ((j + 1) * pageSize)) {
                    break;
                }
            }
            listArray.add(subList);
        }
        return listArray;
    }

    public static <T> List<T> splitList(List<T> list, int start, int pageSize) {
        if (splitList(list, pageSize).size() != 0)
            return splitList(list, pageSize).get(start - 1);
        else
            return new ArrayList<>();
    }

    public static void main(String[] args) {
        List<String> strings = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            strings.add(String.valueOf(i));
        }
        List<String> res = splitList(strings, 1, 10);
        assert res.size() > 0;
    }
}
